package com.etc.controller;

import com.etc.entity.Category;
import com.etc.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


// 分类控制器
@Controller
@RequestMapping("/c")
public class CategoryController {
	@Autowired
    CategoryService categoryService;
	// 根据page获取分类
    @RequestMapping("/queryAll/{page}")
    public ModelAndView queryAllUsers(@PathVariable("page") int page){
      List<Category> list=  categoryService.queryAll(0);
        ModelAndView mv  = new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("index");
    return mv;
    }
    
    // 显示主页
    @RequestMapping("/queryUsers1")
    public ModelAndView querysUsers1(){
    	ModelAndView mv  = new ModelAndView();
        mv.setViewName("index2");
    return mv;
    }
    
    // 获取指定page的分类数组
    @RequestMapping("/queryC2/{page}")
    public ModelAndView querysCategory2(@PathVariable("page") int page){
      List<Category> list=  categoryService.queryAll(page*5);
      
        ModelAndView mv  = new ModelAndView();
        mv.addObject("list",list);
        int totalCount = categoryService.queryAll2().size();
        int pageCount = totalCount / 5;
        if ( totalCount % 5 != 0) {
        	pageCount++;
        }
        
        List<Integer> pageList = new ArrayList<Integer>();
        for (int i = 0 ;i < pageCount; i++) {
        	pageList.add(i);
        }
        mv.addObject("pageList", pageList);
        mv.addObject("totalCount",totalCount);
        
        mv.setViewName("Category_Manage");
        
    return mv;
    }
    
    // 添加
    @RequestMapping("/insert")
    public ModelAndView insert(Category c){
    	categoryService.insert(c);
        return this.querysCategory2(0);
    
    }
    // 插入
    @RequestMapping("/insertc")
    public ModelAndView insert2(Category c){
    	ModelAndView mv  = new ModelAndView();
        mv.setViewName("Category_Add");
        return mv;
    }
    // 根据名字查找
    @RequestMapping("/queryCUsersFilter")
    public ModelAndView queryCUsersFilter1(String name) {
    	if (StringUtils.isEmpty(name)) {
    		return querysCategory2(0);
    	}
    	ModelAndView mv  = new ModelAndView();
    	List<Category> categories = categoryService.queryAll2();
    	List<Category> cc = categories.stream().filter(i->i.getCtitle().contains(name)).collect(Collectors.toList());
    	
    	for (Category c : cc) {
 		   if (c.getParentId() == 0) {
 			   c.setParent("无父类");
 		   } else {
 			   String title = categories.stream().filter(item->item.getCid() == c.getParentId()).findFirst().get().getCtitle();
 			   c.setParent(title);
 			   
 		   }
 	   }
    	
    	
    	mv.addObject("list",cc);
    	int totalCount = categories.stream().filter(i->i.getCtitle().contains(name)).collect(Collectors.toList()).size();
    	
    	int pageCount = totalCount / 5;
    	List<Integer> pageList = new ArrayList<Integer>();
        for (int i = 0 ;i < pageCount; i++) {
        	pageList.add(i);
        }
        mv.addObject("pageList", pageList);
        mv.addObject("totalCount",totalCount);
        
        mv.setViewName("Category_Manage");
        
    return mv;
    
    }
}
